

import 'dart:io';

import 'package:bt_tool/basic/bt_common_define.dart';
import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_date_tool.dart';
import 'package:bt_tool/basic/bt_file_tool.dart';
import 'package:bt_tool/basic/bt_theme.dart';
import 'package:bt_tool/basic/bt_theme_color.dart';
import 'package:bt_tool/basic/bt_utils.dart';
import 'package:bt_tool/widget/bt_base_btn_widget.dart';
import 'package:bt_tool/widget/bt_check_box.dart';
import 'package:bt_tool/widget/bt_text_field.dart';
import 'package:bt_tool/widget/bt_widget.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/img_path_main_controller.dart';
import '../model/img_path_model.dart';

class ImgPathMainPage extends BTStatefulWidget{

  const ImgPathMainPage({super.key,super.routerConfig});

  @override
  State<StatefulWidget> createState() {
    return ImgPathMainPageState();
  }

}

class ImgPathMainPageState extends BTState<ImgPathMainPage>{

  final ImgPathMainController controller = ImgPathMainController();


  @override
  BTController btCtrl() {
    return controller;
  }

  @override
  Widget build(BuildContext context) {
    if(!controller.isHadPermission && !btIsStrEmpty(controller.currentRootPath.value)){

      return Container(
        alignment: Alignment.center,
        child: BTBtnTextWidget(
          text: "获取目录权限",
          bgColor: BTThemeColor.mainColor,
          textColor: Colors.white,
          onTap: ()=>controller.getPermission(),
        ),
      );
    }

    return Container(
      color: BTThemeColor.contentBgColor,
      padding: const EdgeInsets.all(10),
      child: DropTarget(
        onDragEntered: (d){
          controller.isDragFile.value = true;
        },
        onDragExited: (d){
          controller.isDragFile.value = false;
        },
        onDragDone: (d){
          controller.isDragFile.value = false;
          controller.onDragDone(d);
        },
        child: Stack(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Obx(()=>ListView.builder(

                    itemBuilder: (c,i){
                      ImgPathModel model = controller.dataList[i];
                      return Container(
                        margin: const EdgeInsets.all(10),
                        padding: const EdgeInsets.all(10),
                        color: BTThemeColor.contentBgColor2,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [


                            // const SizedBox(height: 10,),
                            // SelectableText("原路径: ${model.imgPath_1}"),

                            // const SizedBox(height: 10,),
                            // SelectableText("原路径2: ${model.imgPath_2}"),

                            Row(
                              children: [
                                Image.file(File(model.imgPath_1),width: 70,height: 70,),
                                const SizedBox(width: 15,),
                                btIsStrEmpty(model.imgPath_2) ? const SizedBox() : Image.file(File(model.imgPath_2),width: 70,height: 70,),
                              ],
                            ),
                            const SizedBox(height: 10,),
                            Row(
                              children: [
                                SelectableText("目标路径: ${model.resultPath}"),
                                BTBtnTextWidget(
                                  onTap: (){
                                    BTFileTool.openFolder(model.resultPath);
                                  },
                                  text: "打开",
                                  textStyle:  TextStyle(
                                      color: BTThemeColor.mainColor,
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(height: 10,),
                            Row(
                              children: [
                                SelectableText("图片名称: ${model.imgName}${model.extension}"),
                                const SizedBox(width: 5,),
                                BTBtnTextWidget(
                                  onTap: ()=>controller.editName(model),
                                  text: "修改名称",
                                  textStyle:  TextStyle(
                                      color: BTThemeColor.mainColor,
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(height: 10,),
                            Row(
                              children: [
                                SelectableText("flutterConfigStr: ${model.flutterConfigStr}"),
                                const SizedBox(width: 5,),
                                GestureDetector(
                                  onTap: ()=>controller.copy(model),
                                  child: Text(
                                    "复制",
                                    style:  TextStyle(
                                        color: BTThemeColor.mainColor,
                                        fontSize: 14,
                                        fontWeight: FontWeight.bold
                                    ),
                                  ),
                                )
                              ],
                            ),
                            const SizedBox(height: 10,),
                            Text("时间:${BTDateTool.formatter(BTDateTool.YYYYMMDDHHmmss, BTDateTool.fromSeconds(model.time))}"),
                            const SizedBox(height: 10,),
                            Row(
                              children: [
                                BTBtnTextWidget(
                                  text: "写入数据",
                                  textStyle: const TextStyle(
                                      color: Colors.white,
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold
                                  ),
                                  bgColor: BTThemeColor.mainColor,
                                  onTap: ()=>model.saveImgIos(),
                                ),
                                const SizedBox(width: 10,),

                                BTBtnTextWidget(
                                  text: "删除文件",
                                  textStyle: const TextStyle(
                                      color: Colors.white,
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold
                                  ),
                                  bgColor: BTThemeColor.mainColor,
                                  onTap: ()=>controller.deleteFile(model),
                                ),
                                const SizedBox(width: 10,),
                                BTBtnTextWidget(
                                  text: "删除数据",
                                  textStyle: const TextStyle(
                                      color: Colors.white,
                                      fontSize: 14,
                                      fontWeight: FontWeight.bold
                                  ),
                                  bgColor: BTThemeColor.mainColor,
                                  onTap: ()=>controller.deleteDataAlert(model),
                                ),
                              ],
                            )
                          ],
                        ),

                      );
                    },
                    itemCount: controller.dataList.length,

                  )),
                ),
                const SizedBox(height: 10,),
                Row(
                  children: [
                    const SizedBox(width: 10,),
                    Obx(()=>SelectableText("图片资源根路径: ${controller.currentRootPath.value.isEmpty ? "暂无" : controller.currentRootPath.value}")),
                    BTBtnTextWidget(
                      text: "选择",
                      textStyle: TextStyle(
                          color: BTThemeColor.mainColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold
                      ),
                      onTap: ()=>controller.pickPath(),
                    ),
                  ],
                ),
                Row(
                  children: [
                    const SizedBox(width: 10,),
                    Obx(()=>SelectableText("目标文件夹: ${controller.currentFolder.value.isEmpty ? "暂无" : controller.currentFolder.value}")),
                    BTBtnTextWidget(
                      text: "选择",
                      textStyle: TextStyle(
                          color: BTThemeColor.mainColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold
                      ),
                      onTap: ()=>controller.pickFolder(),
                    ),
                    BTBtnTextWidget(
                      text: "创建",
                      textStyle: TextStyle(
                          color: BTThemeColor.mainColor,
                          fontSize: 14,
                          fontWeight: FontWeight.bold
                      ),
                      onTap: ()=>controller.createFolder(),
                    ),
                  ],
                ),
                Row(
                  children: [
                    const SizedBox(width: 10,),
                    Obx(()=>BTCheckBox(
                        isSelect: controller.isIosStyle.value,
                        title: "是否为iOS模式（需要区分2x和3x文件夹）",
                        clickCallback: (obj){
                          controller.isIosStyle.value = !controller.isIosStyle.value;
                        },
                        normalImg: Image.asset(btPngPath("bt/bt_check_box_nor")),
                        selectImg: Image.asset(btPngPath("bt/bt_check_box_sel")))
                    ),
                  ],
                ),
                const SizedBox(height: 10,),
                Row(
                  children: [
                    const SizedBox(width: 10,),
                    BTBtnTextWidget(
                      text: "导入图片",
                      bgColor: BTThemeColor.mainColor,
                      textColor: Colors.white,
                      onTap: ()=>controller.importImg(),
                    ),

                    const SizedBox(width: 10,),
                    BTBtnTextWidget(
                      text: "刷新数据",
                      bgColor: BTThemeColor.mainColor,
                      textColor: Colors.white,
                      onTap: ()=>controller.refreshData(),
                    ),
                  ],
                )
              ],
            ),
            Obx(()=>Visibility(
              visible: controller.isDragFile.value,
              child: Container(
                alignment: Alignment.center,
                constraints: const BoxConstraints.expand(),
                color: BTThemeColor.contentBgColor2,
                child: const Text("拖拽到此区域"),
              ),
            )),
          ],
        ),
      ),
    );
  }

}

